package Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

import Dao.StatusDao;
import Dao.TokenDao;
import Dao.WeiboStatus;
import Dao.WeiboToken;
import Data.Status;
import Data.Users;

public class ServiceTest implements Job{

    //private static String poolName = "testPool" ;
    
    private ExecutorService executorService = Executors.newFixedThreadPool(3);
    
    private StatusDao statusDao ;
    
    private List<Status> status ;
    
    private List<Users> users ;
    
    private TokenDao tokenDao ;
    
    public static void main(String[] args) {
	// TODO Auto-generated method stub
		
	//executorService = Executors.newFixedThreadPool(3) ;
		
	SchedulerFactory schedulerFactory = new StdSchedulerFactory() ;
	
	Scheduler scheduler ;
	
	try {
	    
	    scheduler = schedulerFactory.getScheduler() ;
	    
	    JobDetail jobDetail = new JobDetail("jobDetail-sl", "jobDetailGroup-s1", ServiceTest.class) ;
	    
	    SimpleTrigger simpleTrigger = new SimpleTrigger("simpleTrigger", "triggerGroup-s1") ;
	    
	    long ctime = System.currentTimeMillis() ;
	    
	    simpleTrigger.setStartTime(new Date(ctime));
	    
	    simpleTrigger.setRepeatInterval(10000);
	    
	    simpleTrigger.setRepeatCount(2);
	    
	    scheduler.scheduleJob(jobDetail ,simpleTrigger) ;
	    
	    scheduler.start(); 
	    
	} catch (SchedulerException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	
	
	/*CrawlData crawlData = new CrawlData("2797623222", "Weibo") ;
	crawlData.crawlWeiboFriends();
	//crawlData.crawlWeibo();
	crawlData.run();*/
	
	/*WeiboRequest weiboRequest = new WeiboRequest("2797623222") ;
	weiboRequest.setMethod("Weibo");
	weiboRequest.setParam(null);
	Response weiboFun = weiboRequest.getRespones();
	weiboFun.parse();*/
	
	
	/*String a = String.format("friendships/friends.json?access_token=%s&uid=%s", "2.00n5j1xC2VRMOE5b1ac9a0f7KHPvRE" , "2709010565" ) ;
	
	System.out.println(a);*/
	
    }

    
    public void addRunable( Runnable r ){
	executorService.execute(r);
    }
    
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
	// TODO Auto-generated method stub
	tokenDao = new WeiboToken() ;
	users = tokenDao.querUsers() ;
	for( int i = 0 ; i <users.size() ; i++){
	    CrawlData r = new CrawlData(users.get(i), "Weibo") ;
	    r.crawlWeibo();
	    addRunable(r);
	}
	
	//System.out.println();
    }

    
    
    private TokenDao WeiboToken() {
	// TODO Auto-generated method stub
	return null;
    }
    
}
